package com.baitiaojun.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baitiaojun.common.enums.impl.DataSourceTypeEnums;
import com.baitiaojun.datasource.core.datasource.DynamicRoutingDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
public class DynamicDataSourceConfig {

    @Autowired
    private DynamicDataSourceProperties dataSourceProperties;

    /**
     * 初始化主数据库
     * @return defaultDB
     */
    @ConditionalOnProperty(prefix = "spring.datasource", name = "default_db.enable", havingValue = "true", matchIfMissing = true)
    public Map<String, Object> defaultDB() {
        DruidDataSource druidDataSource = dataSourceProperties.getDefaultDB();
        Map<String, Object> defaultDB = new HashMap<>();
        defaultDB.put("driverClassName", druidDataSource.getDriverClassName());
        defaultDB.put("enable", druidDataSource.isEnable());
        defaultDB.put("url", druidDataSource.getUrl());
        defaultDB.put("username", druidDataSource.getUsername());
        defaultDB.put("password", druidDataSource.getPassword());
        return defaultDB;
    }

    /**
     * 初始化从数据库
     * @return slaveDB
     */
    @ConditionalOnProperty(prefix = "spring.datasource", name = "slave_db.enable", havingValue = "true", matchIfMissing = true)
    public Map<String, Object> slaveDB() {
        DruidDataSource druidDataSource = dataSourceProperties.getSlaveDB();
        Map<String, Object> slaveDB = new HashMap<>();
        slaveDB.put("driverClassName", druidDataSource.getDriverClassName());
        slaveDB.put("enable", druidDataSource.isEnable());
        slaveDB.put("url", druidDataSource.getUrl());
        slaveDB.put("username", druidDataSource.getUsername());
        slaveDB.put("password", druidDataSource.getPassword());
        return slaveDB;
    }

    /**
     * 初始化的时候设置DEFAULT_DB为默认目标数据源
     */
    @Bean
    public DynamicRoutingDataSource dynamicRoutingDataSourceHelper() {
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
        DataSource defaultDataSource = dynamicRoutingDataSource.createDataSource(defaultDB());
        DataSource slaveDataSource = dynamicRoutingDataSource.createDataSource(slaveDB());
        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DataSourceTypeEnums.DEFAULT_DB.getName(), defaultDataSource);
        targetDataSource.put(DataSourceTypeEnums.SLAVE_DB.getName(), slaveDataSource);
        dynamicRoutingDataSource.setTargetDataSources(targetDataSource); //设置目标数据源
        dynamicRoutingDataSource.setDefaultTargetDataSource(defaultDataSource); //设置默认数据源
        return dynamicRoutingDataSource;
    }
}
